//
//  AppDelegate.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/16.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //测试数据库
//        SQLiteManager.sharedManager
        
        //测试EmotionViewModel
//        let emotionViewModel = EmotionKeyboardViewModel.sharViewModel
        
//        print(emotionViewModel.defaultEmotion)
//        print(emotionViewModel.emojiEmotion)
//        print(emotionViewModel.lxhEmotion)
//        print(emotionViewModel.allEmotions)
        
        setupAFNetworking()
        
        // 接收跳转控制器的通知
        // object参数:是监听某个对象发来的通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootViewController:", name: XHChangeRootViewNotification, object: nil)
        
        //设置窗口大小
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //指定根控制器
        if UserAccountViewModel.sharedViewModel.isLogin {
            
            window?.rootViewController = WelcomeViewController()
            
        }else{
            
            window?.rootViewController = MainTabBarController()
            
        }
        
        //设置为主窗口并可见
        window?.makeKeyAndVisible()
        
        //设置导航栏的外观代理
        setupNavAppearence()
        
        return true
    }
    
    private func setupAFNetworking(){
        
        //网络指示器
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        
        //设置缓存
        /*
        <li>Memory capacity: 4 megabytes (4 * 1024 * 1024 bytes)
        <li>Disk capacity: 20 megabytes (20 * 1024 * 1024 bytes)
        <li>Disk path: <nobr>(user home directory)/Library/Caches/(application bundle id)</nobr>
        */
        NSURLCache.setSharedURLCache(NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil))
    }
    
    @objc private func changeRootViewController(noti:NSNotification){
        
        //切换控制器
        if (noti.object != nil){
            
            //跳转到首页
            window?.rootViewController = MainTabBarController()
            
        }else{
            
            //跳转到欢迎界面
            window?.rootViewController = WelcomeViewController()
        }
        
    }
    
    // 设置全局的外观颜色配置，需要在 AppDelegate 里设置 (越早设置越好)
    private func setupNavAppearence(){
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        //修改导航栏标题文字的大小,若要全部修改,则应该在 AppDelegate 中设置
        UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName:UIFont.systemFontOfSize(18)]
    }
    
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    func applicationDidEnterBackground(application: UIApplication) {
        //程序一进入后台 就清空七天前的数据
        StatusDAL.clearCache()
    }
    
    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    
}

